﻿'***********************************************************************
'Magic Chimney Imp (MCI)
'Copyright (C) 2011  David Frendin (david.frendin@gmail.com)
'
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.
'
'You should have received a copy of the GNU General Public License
'along with this program.  If not, see <http://www.gnu.org/licenses/>.
'***********************************************************************
Imports System.Text

Public Class Deck

    Public MainDeck As List(Of CardSlot)
    Public SideBoard As List(Of CardSlot)
    Public FileName As String
    Public DeckName As String = String.Empty
    Public DeckNotes As String = String.Empty

    Public Structure CardSlot
        Dim Card As CardPool.Card
        Dim Quantity As Integer
    End Structure

    Public Sub LoadDeck(ByVal FileName As String)
        Dim ds As New DataSet
        ds.ReadXml(FileName)

        MainDeck.Clear()
        Try
            For Each row In ds.Tables("card").Rows
                Dim MultiverseId As String = row("multiverseid").ToString
                Dim cs As New CardSlot
                cs.Card = CardPool.GetCard(MultiverseId)
                cs.Quantity = CInt(row("quantity").ToString)
                MainDeck.Add(cs)
            Next

            DeckName = ds.Tables("meta").Rows(0)("name").ToString
            DeckNotes = ds.Tables("meta").Rows(0)("notes").ToString

            Me.FileName = FileName
        Catch ex As Exception
            MsgBox("Error loading deck: " & ex.Message)
        End Try
    End Sub


    Public Sub Sync(ByVal CardRows As DataGridViewRowCollection)
        MainDeck.Clear()
        For Each row As DataGridViewRow In CardRows
            Dim drv As DataRowView = row.DataBoundItem
            Dim MultiverseId As String = row.Cells("MultiverseId").Value.ToString
            Dim Quantity As Integer = CInt(drv.Row("Quantity").ToString)

            Dim cs As New CardSlot
            cs.Card = CardPool.GetCard(MultiverseId)
            cs.Quantity = Quantity
            MainDeck.Add(cs)
        Next
    End Sub

    Public Sub Sync(ByRef MainGrid As ComponentFactory.Krypton.Toolkit.KryptonDataGridView)
        Dim MyDeck As New DataTable
        MyDeck.Columns.Add("MultiverseId")
        MyDeck.Columns.Add("Quantity")
        MyDeck.Columns.Add("Name")
        MyDeck.Columns.Add("Type")
        MyDeck.Columns.Add("Color")
        MyDeck.Columns.Add("Cost")

        For Each cs As CardSlot In MainDeck
            Dim nr As DataRow = MyDeck.NewRow
            nr("Quantity") = cs.Quantity
            nr("Name") = cs.Card.Name
            nr("MultiverseId") = cs.Card.MultiverseId
            nr("Color") = cs.Card.Color
            nr("Type") = cs.Card.Type
            nr("Cost") = cs.Card.Cost
            MyDeck.Rows.Add(nr)
        Next
        MainGrid.AutoGenerateColumns = False
        MainGrid.DataSource = MyDeck

    End Sub

    Public Sub Save(ByVal FileName As String)
        Dim sb As New StringBuilder
        sb.AppendLine("<deck>")
        sb.AppendLine("<meta>")
        sb.AppendLine(String.Concat("<version>1</version>"))
        sb.AppendLine(String.Concat("<name>", DeckName, "</name>"))
        sb.AppendLine(String.Concat("<notes>", DeckNotes, "</notes>"))
        sb.AppendLine("</meta>")
        sb.AppendLine("<main>")
        For Each cs As CardSlot In MainDeck
            sb.AppendLine(String.Concat("<card multiverseid=""", cs.Card.MultiverseId & """ quantity=""" & cs.Quantity.ToString & """ />"))
        Next
        sb.AppendLine("</main>")
        sb.AppendLine("<sideboard>")
        For Each cs As CardSlot In SideBoard
            sb.AppendLine(String.Concat("<card multiverseid=""", cs.Card.MultiverseId & """ quantity=""" & cs.Quantity.ToString & """ />"))
        Next
        sb.AppendLine("</sideboard>")
        sb.AppendLine("</deck>")

        If IO.File.Exists(FileName) Then
            Kill(FileName)
        End If
        Using sw As New IO.StreamWriter(FileName, IO.FileMode.Create)
            sw.Write(sb.ToString)
        End Using
    End Sub

    Public Sub New()
        MainDeck = New List(Of CardSlot)
        SideBoard = New List(Of CardSlot)
    End Sub
End Class
